package com.travel.service.WeiXin;

import com.travel.common.WeiXin;
import com.travel.dao.access_token.Access_TokenMapper;
import com.travel.pojo.WeiXin.GetAccess_token;
import com.travel.pojo.WeiXin.SubscribeMessage;
import com.travel.pojo.WeiXin.WeiXinLogin;
import com.travel.service.HttpClient;
import net.sf.json.JSONObject;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

@Service
public class WeiXinApi {

    @Autowired
    HttpClient httpClient;

    @Autowired
    Access_TokenMapper access_tokenMapper;

    /**
     *从微信小程序api中获取openid
     * @param code 前端授权获取
     */
    public WeiXinLogin getOpenIdAndUnionId(String code){
            String url="https://api.weixin.qq.com/sns/jscode2session?appid="+WeiXin.getAppID()+"&secret="+WeiXin.getAppSecret()+"&js_code="+code+"&grant_type=authorization_code";
            JSONObject data= httpClient.client(url);
            WeiXinLogin wxuser= (WeiXinLogin)JSONObject.toBean(data,WeiXinLogin.class);
            return wxuser;
    }

    /**
     * 从微信小程序api中获取到access_token
     */
    @Scheduled(cron = "0 0 0/3 * * ?")
    @Scheduled(cron = "0 55 1/3 * * ?")
    public String getAccess_token(){
            String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+WeiXin.getAppID()+"&secret="+WeiXin.getAppSecret();
            JSONObject data= httpClient.client(url);
            GetAccess_token getAccessToken=(GetAccess_token)JSONObject.toBean(data,GetAccess_token.class);
            access_tokenMapper.updateAccess_token(getAccessToken.getAccess_token());
            return getAccessToken.getAccess_token();
    }



    /**
     *远程调用微信小程序订阅消息api,发送微信订阅消息
     * @param subscribeMessage 微信小程序订阅消息中需要的参数
     */
    public void sendSubscribeMessage(SubscribeMessage subscribeMessage){
        String openid= subscribeMessage.getOpenid();
        String sendData_thing2=subscribeMessage.getThing2();
        String sendData_thing6=subscribeMessage.getThing6();
        String sendData_time4=subscribeMessage.getTime4();
        String sendData_thing5=subscribeMessage.getThing5();
        String coordinate=subscribeMessage.getCoordinate();
        String access_token=access_tokenMapper.getAccess_token();
        String url="https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+access_token;
        Map<String,Object> map=new HashMap<>();
        //发送对象
        map.put("touser",openid);
        //模板id
        map.put("template_id","GBlGRp9gZF0S70SCGl6N1A1GMgs98F9F-UazZu9z2JE");
        //点击模板跳转页面,携带参数坐标
        map.put("page","pages/index/index");
        //跳转小程序类型
        //formal正式版，trial体验版，developer开发版
        map.put("miniprogram_state","formal");
        //进入小程序查看的语言类型
        map.put("lang","zh_CN");


        Map<String,Object> data=new HashMap<>();

        //封装需要发送的数据data
        Map<String,String> thing2=new HashMap<>();
        thing2.put("value",sendData_thing2);
        data.put("thing2",thing2);

        Map<String,String> thing6=new HashMap<>();
        thing6.put("value",sendData_thing6);
        data.put("thing6",thing6);

        Map<String,String> time4=new HashMap<>();
        time4.put("value",sendData_time4);
        data.put("time4",time4);

        Map<String,String> thing5=new HashMap<>();
        thing5.put("value",sendData_thing5);
        data.put("thing5",thing5);

        //将data保存到远程调用url需要的请求参数中
        map.put("data",data);


        httpClient.client(url,map);
    }

}
